var gulp = require('gulp');
var browserify = require('browserify');
var babelify = require('babelify');
var source = require('vinyl-source-stream');
var sass = require('gulp-sass');
var plumber = require('gulp-plumber');//事件
var notify = require('gulp-notify');//错误
var uglify = require('gulp-uglify');//压缩
var streamify = require('gulp-streamify');
gulp.task('buildJS', function () {
    return browserify('./source/app.js')
        .transform(babelify)
        .bundle()
        .on('error', function(err){
            var args = Array.prototype.slice.call(arguments);
            notify.onError({
                title: "Build JS Error",
                message: "<%= error.message %>"
            }).apply(this, args);
            this.emit('end');
        })
        .pipe(source('oni.js'))
        .pipe(streamify(uglify()))
        .pipe(gulp.dest('./build/'))
});
gulp.task('buildCSS', function () {
    gulp.src('./style/app.scss')
        .pipe(plumber({errorHandler: notify.onError('Error: <%= error.message %>')}))
        .pipe(sass())
        .pipe(gulp.dest('./build'))
});
gulp.task('watchStyle', function () {
    gulp.watch('./style/**/*',['buildCSS'])
})
gulp.task('watchSource', function () {
    gulp.watch('./source/**/*', ['buildJS'])
});
gulp.task('default',['watchSource','watchStyle']);

